package com.skateboard.reelviewtest.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.skateboard.reelviewtest.R;
import java.util.ArrayList;
import java.util.List;

/**
 * -------------------------------------
 * 作者：腾超杰
 * -------------------------------------
 * 时间：2020/11/25 5:47 PM
 * -------------------------------------
 * 描述：礼物墙排序View
 * -------------------------------------
 * 备注：
 * -------------------------------------
 */
public class GiftWallSortView extends LinearLayout {

    private String[] data;
    private List<GiftWallSortTab> tabs;

    private View sortView;
    private TextView tvSortOpen;
    private ImageView ivSortClose;
    private LinearLayout sorTabLayout;
    private LinearLayout sortTabBar;

    /**
     * 默认显示View的宽度
     */
    private float SHOW_VIEW_WIDTH = 64;

    public GiftWallSortView(Context context) {
        this(context, null);
    }

    public GiftWallSortView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GiftWallSortView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        removeAllViews();
        setOrientation(HORIZONTAL);
        sortView = View.inflate(getContext(), R.layout.giftwall_sort_layout, null);
        tvSortOpen = sortView.findViewById(R.id.tv_sort_open);
        ivSortClose = sortView.findViewById(R.id.iv_sort_close);
        sorTabLayout = sortView.findViewById(R.id.sort_tab_layout);
        sortTabBar = sortView.findViewById(R.id.sort_tab_bar);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(getScreenWidth(), dp2Px(33f));
        layoutParams.setMarginStart(getScreenWidth() - dp2Px(SHOW_VIEW_WIDTH));
        addView(sortView, layoutParams);

        tvSortOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                translation(sortView, -sorTabLayout.getWidth() + dp2Px(SHOW_VIEW_WIDTH));
                translation(tvSortOpen, -dp2Px(SHOW_VIEW_WIDTH));
                translation(sorTabLayout, -dp2Px(SHOW_VIEW_WIDTH));
            }
        });

        ivSortClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                translation(sortView, 0);
                translation(tvSortOpen, 0);
                translation(sorTabLayout, 0);
            }
        });
    }

    /**
     * 平移动画
     *
     * @param v
     * @param values
     */
    private void translation(View v, float... values) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(v, "translationX", values);
        animator.setDuration(1000);
        animator.start();
    }

    public void setSortTabData(String[] data) {
        this.data = data;
        addTabView(data);
    }

    /**
     * 动态添加筛选View
     *
     * @param data
     */
    private void addTabView(final String[] data) {
        if (tabs == null) {
            tabs = new ArrayList<>();
        }
        tabs.clear();
        if (data == null || data.length == 0) return;
        if (sortTabBar != null) {
            sortTabBar.removeAllViews();
            LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.setMarginEnd(dp2Px(20f));
            for (int i = 0; i < data.length; i++) {
                final GiftWallSortTab tab = new GiftWallSortTab(getContext());
                if (i == 0) {
                    tab.selected(data[i]);
                } else {
                    tab.unSelected(data[i]);
                }
                tab.setTag(i);
                tab.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = (int) v.getTag();
                        initTabView(position);
                        if(onTabItemClickListener != null){
                            onTabItemClickListener.onTabItemClick(v,position);
                        }
                    }
                });
                sortTabBar.addView(tab, layoutParams);
                tabs.add(tab);
            }
        }
    }

    /**
     * 设置tab选中状态
     * @param position
     */
    private void initTabView(int position) {
        for (int i = 0; i < tabs.size(); i++) {
            if(i == position){
                tabs.get(i).selected(data[i]);
            } else {
                tabs.get(i).unSelected(data[i]);
            }
        }
    }

    private OnTabItemClickListener onTabItemClickListener;

    //设置回调接口
    public interface OnTabItemClickListener{
        void onTabItemClick(View view, int position);
    }

    public void setOnTabItemClickListener(OnTabItemClickListener listener) {
        this.onTabItemClickListener = listener;
    }

    public int dp2Px(float dp) {
        final float scale = getContext().getResources().getDisplayMetrics().density; //当前屏幕密度因子
        return (int) (dp * scale + 0.5f);
    }

    public int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

}
